//Source file: D:\\code\\com\\ce\\pms\\house\\web\\MeterAction.java

package com.ce.pms.house.web;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.charge.service.IMeterDataRegisterService;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.service.IMeterService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.house.vo.MeterVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述: 对走表进行维护
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 许海堆 Created on 2008-7-8
 * @author update on
 */
public class MeterAction extends PmsBaseAction {

	/**
	 * Function:初始化增加房屋走表
	 *
	 * @param
	 * @return Create author:xuhaidui Create on:2008-7-8 Edit author: Edit on:
	 *         Why:
	 */
	public String initAddHouseMeter(@Read(key = "areaId")
	Long areaId) {

		List<BuildingVO> list = buildingService.getBuildingListByAreaId(areaId);
		this.getRequest().setAttribute("buildingList", list);
		List empty = new ArrayList();
		if (list.size() < 26) {
			for (int i = list.size() + 1; i < 26; i++) {
				empty.add(i);
			}
		} else {
			for (int i = list.size() % 5 + 1; i < 6; i++) {
				empty.add(i);
			}
		}
		this.getRequest().setAttribute("emptyList", empty);

		this.getRequest().setAttribute("meterTypeList",
				meterService.getMeterTypeList());

		return "addHouseMeter";
	}

	/**
	 * 功能描述:增加房屋走表
	 *
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui Create on:2008-7-17 Edit author: Edit on: Why:
	 */
	public String addHouseMeter(@Read
	MeterVO meterVO) {

		meterVO.setCreator(this.getUserId());
		meterService.addMeterBatch(meterVO);
		this.getRequest().setAttribute("flag", "addMeter");

		return "onlyRefreshHouse";

	}

	/**
	 * 功能描述:获得房间走表详细列表
	 *
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui Create on:2008-7-17 Edit author: Edit on: Why:
	 */
	public String getMeterListDetailByHouseId(@Read(key = "houseId")
	Long houseId, @Read(key = "ec_crd", defaultValue = "5")
	int pageSize, @Read(key = "ec_p", defaultValue = "1")
	int pageNo) {
		Page page = meterService.queryPageMeterListByHouseId(houseId, pageNo,
				pageSize);
		this.getRequest().setAttribute("houseMeterListDetai", page.getResult());

		if(!BlankUtil.isBlank(page.getResult())){
			this.getRequest().setAttribute("totalRows",
					new Integer((int) page.getTotalCount()));
			}else{
				this.getRequest().setAttribute("totalRows",
						new Integer(0));
			}
			this.getRequest().setAttribute("houseId", houseId);
		return "houseMeterDetail";
	}

	/**
	 * Function:初始化房屋走表维护List
	 *
	 * @param
	 * @return Create author:胡煦 Create on:2008-7-21 Edit author: Edit on: Why:
	 */
	public String initMeterMaintenanceList(@Read(key = "houseId")
	Long houseId, @Read(key = "ec_crd", defaultValue = "5")
	int pageSize, @Read(key = "ec_p", defaultValue = "1")
	int pageNo) {
		Page page = meterService.queryPageMeterListByHouseId(houseId, pageNo,
				pageSize);
		this.getRequest().setAttribute("houseMeterList", page.getResult());
		if(!BlankUtil.isBlank(page.getResult())){
		this.getRequest().setAttribute("totalRows",
				new Integer((int) page.getTotalCount()));
		}else{
			this.getRequest().setAttribute("totalRows",
					new Integer(0));
		}
		this.getRequest().setAttribute("houseId", houseId);
		return "houseMeterList";
	}

	/**
	 * Function:初始化修改走表底数页面
	 *
	 * @param
	 * @return Create author:胡煦 Create on:2008-7-21 Edit author: Edit on: Why:
	 */
	public String initMeterRadixEdit(@Read(key = "meterId")
	Long meterId) {
		MeterVO meterVO = meterService.findMeterVOBy(meterId);
		this.getRequest().setAttribute("meterVO", meterVO);
		this.getRequest().setAttribute("meterId", meterId);
		return "initMeterRadixEdit";
	}

	/**
	 * 功能描述:验证走表读数是否大于归零值<br>
	 * @param chargeDataId
	 * Create author:胡煦 <br>
	 * Create on:2008-10-17  <br>
	 */
	public void validateAdjustable(@Read(key="chargeDataId")Long chargeDataId){
		meterDataRegisterService.checkAjustMeterDataState(chargeDataId);
	}

	/**
	 * 方法描述: 验证该房间是否处于退伙中
	 * @author 胡煦
	 * Create on:2008-12-3
	 * @param houseId
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkTheHouseIsInGoout(@Read(key = "houseId")
			Long houseId){
		//验证该房间是否处于退伙中
		meterService.checkTheHouseIsInGoout(houseId);
	}


	/**
	 * Function:初始化更换走表页面
	 *
	 * @param
	 * @return Create author:胡煦 Create on:2008-7-21 Edit author: Edit on: Why:
	 */
	public String initMeterInstead(@Read(key = "meterId")
	Long meterId) {
		MeterVO oldMeterVO = meterService.findMeterVOBy(meterId);
		MeterVO meterVO = new MeterVO();
		if(BlankUtil.isBlank(oldMeterVO.getLastNumer())){
			oldMeterVO.setLastNumer(oldMeterVO.getBase());
		}
		// meterVO.setCreator(this.getUserId());
		meterVO.setLastNumer(oldMeterVO.getLastNumer());
		meterVO.setMeterTypeId(oldMeterVO.getMeterTypeId());
		meterVO.setMeterTypeName(oldMeterVO.getMeterTypeName());
		meterVO.setHouseId(oldMeterVO.getHouseId());
		meterVO.setName(meterService.getNewMeterName(meterVO));
		// meterVO = meterService.addMeter(meterVO);
		this.getRequest().setAttribute("oldMeterVO", oldMeterVO);
		this.getRequest().setAttribute("meterVO", meterVO);
		this.getRequest().setAttribute("meterId", meterId);

		return "initMeterInstead";
	}

	/**
	 * @param houseId
	 * @return String
	 * @roseuid 485A07A7031C
	 */
	public String searchMeterList(Long houseId) {
		return null;
	}

	/**
	 * @param meterId
	 * @return String
	 * @roseuid 485A07E30148
	 */
	public String searchMeterById(Long meterId) {
		return null;
	}

	/**
	 *
	 * 功能描述: 修改走表底数
	 *
	 * @author 胡煦 Created on:2008-7-17
	 *
	 * 修改原因：
	 * @param meterVo
	 * @return MeterVO 返回结果
	 */
	public String editMeterRadix(@Read
	MeterVO meterVO) {
		meterVO.setModifier(this.getUserId());
		meterService.editMeterRadix(meterVO);
		this.getRequest().setAttribute("flag", "editMeterRadix");
		this.renderText("true");
		return "editMeterRadix";
	}

	/**
	 *
	 * 功能描述: 更换走表
	 *
	 * @author 胡煦 Created on:2008-7-17
	 *
	 * 修改原因：
	 * @param meterVo
	 * @return MeterVO 返回结果
	 */
	public String changeMeter(@Read
	MeterVO meterVO) {
		MeterVO oldMeterVO = meterService.findMeterVOBy(meterVO.getOldKeyword());
		meterVO.setCreator(this.getUserId());
		meterVO.setMeterTypeId(oldMeterVO.getMeterTypeId());
		meterVO.setMeterTypeName(oldMeterVO.getMeterTypeName());
		meterVO.setHouseId(oldMeterVO.getHouseId());
		meterVO.setOwnersStr(oldMeterVO.getOwnersStr());
		meterVO.setLastNumer(oldMeterVO.getLastNumer()==null?oldMeterVO.getBase():oldMeterVO.getLastNumer());
		meterVO.setName(meterService.getNewMeterName(meterVO));
		meterService.changeMeter(meterVO);
		this.getRequest().setAttribute("flag", "changeMeter");
		this.renderText("true");
		return "changeMeter";
	}

	private IBuildingService buildingService;

	private IMeterService meterService;

	private IMeterDataRegisterService meterDataRegisterService;

	public void setMeterService(IMeterService meterService) {
		this.meterService = meterService;
	}

	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	/**
	 * @param meterDataRegisterService the meterDataRegisterService to set
	 */
	public void setMeterDataRegisterService(
			IMeterDataRegisterService meterDataRegisterService) {
		this.meterDataRegisterService = meterDataRegisterService;
	}

}
